import axios from 'axios'
import { ElMessage, ElNotification } from 'element-plus';
import {storeA} from '../piniaStore/storeA'
const piniaStore = storeA()
function takeorder(cartfoods:[],shopname:string){
    var data = JSON.stringify({
        "ddaddress": piniaStore.user.Address,
        "user":piniaStore.user.Username ,
        "host": shopname,
        "context": "随便",
        "order": cartfoods
        // [
        //    {
        //       "name": "牛逼面",
        //       "price": 10,
        //       "url": "http://123.60.90.6/i/2023/02/02/2fmlwl-4.jpg",
        //       "business": "老麻抄手",
        //       "text": "可以",
        //       "num": 1
        //    }
        // ]
     });
     
     var config = {
        method: 'post',
        url: 'http://wm.tocode.store/user/getdd',
        headers: { 
           'token': piniaStore.user.token, 
           'Content-Type': 'application/json'
        },
        data : data
     };
     
     axios(config)
     .then(function (response) {
        console.log(JSON.stringify(response.data));
        ElNotification({
            title: '下单成功！',
            message: '请等待商家配送',
            type: 'success',
          })
     })
     .catch(function (error) {
        console.log(error);
     });
}
//获取商店的订单
function getOrder(name:string){
    return new Promise((resolve, reject)=>{
        var config = {
            method: 'get',
            url: 'http://wm.tocode.store/shop/order?name='+name,
            headers: { 
               'token': piniaStore.user.token, 
            }
         };
         
         axios(config)
         .then(function (response) {
            // console.log(JSON.stringify(response.data));
            resolve(response.data)
         })
         .catch(function (error) {
            console.log(error);
            reject()
         });
         
    })
}
function finishOrder(shopname:string,id:string){
   var config = {
      method: 'put',
      url: 'http://wm.tocode.store/shop/col?id='+id+'&shop='+shopname,
      headers: { 
         'token': piniaStore.user.token, 
      }
   };
   
   axios(config)
   .then(function (response) {
      console.log(JSON.stringify(response.data));
      if(response.data.status==1){
         ElMessage({
            message: '订单已完成！',
            type: 'success',
          })
      }{
         ElMessage({
            message: '发生错误！订单未完成',
            type: 'error',
          })
      }
   })
   .catch(function (error) {
      console.log(error);
   });
}
function getUserOrder(){
   return new Promise((resolve, reject)=>{
      var config = {
         method: 'get',
         url: 'http://wm.tocode.store/user/getOrderByUser?name='+piniaStore.user.Username,
         headers: { 
            'token': piniaStore.user.token, 
         }
      };
      
      axios(config)
      .then(function (response) {
         // console.log(JSON.stringify(response.data));
         resolve(response.data)
      })
      .catch(function (error) {
         console.log(error);
         reject()
      });
      
   })
}

export{
    takeorder,getOrder,finishOrder,getUserOrder
}